﻿--[[
    @file   Frame bottom control.
    @author jameszhai
    @date   2017-06-15
--]]

EOffGMBarUI = 
{
    sUoHelper = "uoBottom\\pnlBtmBar\\uoHelper";        -- 助手设置
    sBtnState = "uoBottom\\pnlBtmBar\\btnState";        -- 游戏状态
    sBtnStart = "uoBottom\\pnlBtmBar\\btnStart";        -- 游戏启动
    sBtnMenu  = "btnGMMenu";

    sBtnRail  = "uoBottom\\pnlBtmBar\\btnRailCfg";
    sBtnDCL   = "uoBottom\\pnlBtmBar\\btnDLCCfg";

    sBtnHovBG = "ONEIMAGE:bottom_bar\\play_hover.tfc;Stretch;0";
    sBtnNrlBG = "ONEIMAGE:bottom_bar\\start.png;Stretch;0;";

    -- 启动中背景
    sStartBG  = "ONEIMAGE:bottom_bar\\starting.png;Stretch;0;";

    -- 运行中背景
    sBtnRunBG = "ONEIMAGE:bottom_bar\\running.png;Stretch;0;";

    -- 服务器不可用状态
    sBtnErrBG = "ONEIMAGE:bottom_bar\\mainting.png;Stretch;0;";
};

TOfflineBtmBar = 
{
    ierd_tgp.tpf_ui.Require("TPFLocalization.lua"); 

    InitView = function(dwRoot)
        local tpfui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        if tpfui ~= nil then
            TOfflineBtmBar.is_english_ver = tpfui:Is_English_ver();
        end;

        TOfflineBtmBar.pPnlThis = ui_utility.get_ctrl_ptr_by_id(dwRoot);
        if nil == TOfflineBtmBar.pPnlThis then
            TGPLog.LogE("OfflineBtmBar", "Get bottom bar fail.");
            return false;
        end;

        TOfflineBtmBar.nBtnStart = ui_utility.get_ctrl_id_by_path(dwRoot,
                                    EOffGMBarUI.sBtnStart);
        if not TOfflineBtmBar.IsValidID(TOfflineBtmBar.nBtnStart) then
            TGPLog.LogE("OfflineBtmBar", "Start button is null.");
            return false;
        end;
        ui_utility.setproperty_by_id(TOfflineBtmBar.nBtnStart, "OnClick",
                                     "TOfflineBtmBar.OnClkStart");
        ui_utility.setproperty_by_id(TOfflineBtmBar.nBtnStart, "OnMouseHover",
                                     "TOfflineBtmBar.OnHovStart");
        ui_utility.setproperty_by_id(TOfflineBtmBar.nBtnStart, "OnMouseLeave",
                                     "TOfflineBtmBar.OnLeaveStart");

        TOfflineBtmBar.nBtnState = ui_utility.get_ctrl_id_by_path(dwRoot,
                            EOffGMBarUI.sBtnState);
        if not TOfflineBtmBar.IsValidID(TOfflineBtmBar.nBtnState) then
            TGPLog.LogE("OfflineBtmBar", "Start button is null.");
            return false;
        end;

        TOfflineBtmBar.nBtnMenu  = ui_utility.get_ctrl_id_by_path(
                                    TOfflineBtmBar.nBtnState, EOffGMBarUI.sBtnMenu);
        if not TOfflineBtmBar.IsValidID(TOfflineBtmBar.nBtnMenu) then
            TGPLog.LogE("OfflineBtmBar", "Start button is null.");
            return false;
        end;
        ui_utility.setproperty_by_id(TOfflineBtmBar.nBtnMenu, "OnClick",
                                     "TOfflineBtmBar.OnClkMenu");

        
        local   dwTempID = ui_utility.get_ctrl_id_by_path(
                                dwRoot, EOffGMBarUI.sBtnRail);
        -- Rail配置按钮
        if not TOfflineBtmBar.IsValidID(dwTempID) then
            TGPLog.LogE("OfflineBtmBar", "Get rail config button fail.");
            return false;
        end;
        ui_utility.setproperty_by_id(dwTempID, "OnClick", "TOfflineBtmBar.OnClkRailCfg");

        -- DLC配置按钮
        TOfflineBtmBar.nBtnDLCCfg = ui_utility.get_ctrl_id_by_path(
                                dwRoot, EOffGMBarUI.sBtnDCL)
        if not TOfflineBtmBar.IsValidID(TOfflineBtmBar.nBtnDLCCfg) then
            TGPLog.LogE("OfflineBtmBar", "Get rail config button fail.");
            return false;
        end;
        ui_utility.setproperty_by_id(TOfflineBtmBar.nBtnDLCCfg, "OnClick", 
                                     "TOfflineBtmBar.OnClkDCLCfg");

        comm_center.reg_service("Svr_ShowStartGame", "TOfflineBtmBar.OnSvrShowBar");

        return true;
    end;

    IsValidID = function(nCtrlID)
        return nil ~= nCtrlID and 0 ~= nCtrlID;
    end;

    OnHovStart = function(pUILib, nBtnID)
        if TOfflineBtmBar.IsValidID(TOfflineBtmBar.nBtnStart) then
            ui_utility.setproperty_by_id(TOfflineBtmBar.nBtnStart, "Background",
                                         EOffGMBarUI.sBtnHovBG);
        end;
    end;

    OnLeaveStart = function(pUILib, nBtnID)
        if TOfflineBtmBar.IsValidID(TOfflineBtmBar.nBtnStart) then
             ui_utility.setproperty_by_id(TOfflineBtmBar.nBtnStart, "Background",
                                            EOffGMBarUI.sBtnNrlBG);
        end;
    end;

    -- 启动游戏
    OnClkStart = function(pUILib, nBtnID)
        if nil ~= TOfflineBtmBar.nCurGameID and 0 ~= TOfflineBtmBar.nCurGameID then
            local pGMLib = base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");

            if nil == pGMLib then
                TGPLog.LogE("OfflineBtmBar", "Start game, game lib is nil");
                return;
            end;

            local   zParam = comm_center.call_service("LoginSvr_GetSelfUin", {});

            pGMLib:launch_game(TOfflineBtmBar.nCurGameID,                   -- 游戏ID
                               0,                                           -- 大区ID
                               zParam.uin,                                  -- UIN
                               ierd_tgp.game_library.LAUNCH_OP_FROM_MAIN_WND,
                               "3100081-1",
                               ierd_tgp.game_library.ZONE_TYPE_FORMAL,
                               "");
        else
            TGPLog.LogE("OfflineBtmBar", "Start game, id invalid.");
        end;
    end;

    OnClkMenu = function(pUILib, nBtnID)
        -- 显示菜单
        local   pMenu = ui_utility.get_ctrl_ptr_by_id(nBtnID);
        if nil == pMenu then
            TGPLog.LogE("OfflineBtmBar", "Get menu button fail.");
            return;
        end;

        local   rtMenu = pMenu:GetRectEx(TRUE,TRUE);
        local   zParam = {};

        zParam["GameID" ] = TOfflineBtmBar.nCurGameID;
        zParam["XOffset"] = rtMenu.right;
        zParam["YOffset"] = rtMenu.top;

        comm_center.call_service("Svr_ShowBtmMenu", zParam);
    end;

    -- 启动游戏服务
    OnSvrShowBar = function(zParam)
        local   nGameID = zParam["GameID"];

        if nil == nGameID or 0 == nGameID then
            TGPLog.LogE("OfflineBtmBar", "Start game, id invalid.")
            return;
        end;

        local   zState = TOfflineModule.GetGameState(nGameID);

        if nil == zState then
            TGPLog.LogE("OfflineBtmBar", "Start game, game state not find:" .. 
                        tostring(nGameID))
            return;
        end;

        TOfflineBtmBar.nCurGameID = nGameID;

        TOfflineBtmBar.UpdateState(zState);
        TGPLog.LogI("OfflineBtmBar", "On start game: " .. tostring(nGameID));        
        TOfflineBtmBar.ShowView();          -- 处理界面
    end;

    -- 更新状态
    UpdateState = function(zState)
        if TOfflineBtmBar.nCurGameID ~= zState.iGame_id then
            TGPLog.LogI("OfflineBtmBar", "Update game state, not current game: ".. 
                        tostring(zState.iGame_id));
            return;
        end;

        if ierd_tgp.game_library.GAME_STATE_RUNNING == zState.iState then
            ui_utility.show_ctrl(TOfflineBtmBar.nBtnStart, false);
            ui_utility.show_ctrl(TOfflineBtmBar.nBtnState, true);
            ui_utility.show_ctrl(TOfflineBtmBar.nBtnMenu, true);

            -- 运行中背景
            ui_utility.setproperty_by_id(TOfflineBtmBar.nBtnState, "Background", 
                                             EOffGMBarUI.sBtnRunBG);
        elseif ierd_tgp.game_library.GAME_STATE_LAUNCHING == zState.iState then
            ui_utility.show_ctrl(TOfflineBtmBar.nBtnStart, false);
            ui_utility.show_ctrl(TOfflineBtmBar.nBtnState, true);
            ui_utility.show_ctrl(TOfflineBtmBar.nBtnMenu, false);

            -- 启动中背景
            ui_utility.setproperty_by_id(TOfflineBtmBar.nBtnState, "Background", 
                                             EOffGMBarUI.sStartBG);
        elseif ierd_tgp.game_library.GAME_STATE_NORMAL == zState.iState then
            ui_utility.show_ctrl(TOfflineBtmBar.nBtnStart, true);
            ui_utility.show_ctrl(TOfflineBtmBar.nBtnState, false);

            -- 开始游戏
            ui_utility.setproperty_by_id(TOfflineBtmBar.nBtnStart, "Background", 
                                         EOffGMBarUI.sBtnNrlBG);
        else
            ui_utility.show_ctrl(TOfflineBtmBar.nBtnStart, false);
            ui_utility.show_ctrl(TOfflineBtmBar.nBtnState, true);
            ui_utility.show_ctrl(TOfflineBtmBar.nBtnMenu, false);
            
            ui_utility.setproperty_by_id(TOfflineBtmBar.nBtnState, "Background",
                                             EOffGMBarUI.sBtnErrBG);
            TGPLog.LogI("OfflineBtmBar", "Game state:" .. zState.iState);
        end;
    end;

    HideView = function()
        if nil ~= TOfflineBtmBar.pPnlThis then
            TOfflineBtmBar.pPnlThis:Show(FALSE);
        end;
    end;

    ShowView = function()
        if nil ~= TOfflineBtmBar.pPnlThis then
            TOfflineBtmBar.pPnlThis:Show(TRUE);
        end;
    end;

    -- Rail 设置
    OnClkRailCfg = function (pUILib, nBtnID)
        -- local pHost = base_utility.get_component_adapter("IRailHost", 
            -- "ierd_tgp.tgp_plugin.GetRailHostAdapter();");

        -- if nil ~= pHost then
            -- pHost:OpenCrossConfigWnd(TOfflineBtmBar.nCurGameID);
        -- else
            -- TGPLog.LogE("OfflineBtmBar", "Click cross setting, RailHost is null.");
        -- end;
        local param = {};
        param["game_id"] = TOfflineBtmBar.nCurGameID;
        event_center.send_event(frame_bottom_event.RAIL_PROPERTIES_BTN_CLICK_EVENT, base_utility.table_to_json(param));
    end;

    -- DLC设置
    OnClkDCLCfg = function (pUILib, nBtnID)
        -- 增加dlc按钮点击逻辑
        comm_center.call_service("Svr_ShowDlcMgr", {});
    end;

    -- 显示DLC按钮
    OnSvrShowDLC = function (zParam)
        if true == zParam["show"] then
            ui_utility.show_ctrl(TOfflineBtmBar.nBtnDLCCfg, true);
        else
            ui_utility.show_ctrl(TOfflineBtmBar.nBtnDLCCfg, true);
        end;
    end;

    pPnlThis   = nil;

    nBtnStart  = 0;
    nBtnState  = 0;
    nBtnMenu   = 0;

    nBtnDLCCfg = 0;

    nCurGameID = 0;
    
    is_english_ver = false;
};
